#include "math.h"     
//#include stdio.h
#include <iostream>  
#include <fstream>
#include <sstream>
#include <map> 
#include <iomanip> 
#include <string.h> 
#include <stdlib.h>

using namespace std;
  string index[97][88];
	string index1;
	string index2[96];
	float indexf[3][96];
	float indexf_CCBQ=0;
  float indexf_PNP=0;
	float indexf_G3P=0;
	double indexd[3][96];
	double indexd_CCBQ=0;
	double indexd_PNP=0;
	double indexd_G3P=0;
	int i=0;
	int j=1;
	int i1,i2,i3;

    std::ifstream fin("scada.txt");
    string str,str1,str2;
//	float DATE,TIME,GenTotP,GenTotQ,CCAP,CCAQ,CCBP,CCBQ,CMCP,CMCQ,IPPP,IPPQ,GongHeP,GongHeQ,ZhuHeAP,ZhuHeAQ,ZhuHeBP,ZhuHeBQ,ZhuAoP,ZhuAoQ,NanAoP,NanAoQ,APP,APQ,MNP,MNQ,DMP,DMQ,SPP,SPQ,PEP,PEQ,TPP,TPQ,NTP,NTQ,VNP,VNQ,CLP,CLQ,COP,COQ,LXP,LXQ,PNP,PNQ,NAP,NAQ,CDP,CDQ,CIP,CIQ,CSP,CSQ,G1P,G1Q,G2P,G2Q,G3P,G3Q,G4P,G4Q,G5P,G5Q,G6P,G6Q,G7P,G7Q,G8P,G8Q,STG34P,STG34Q,STG56P,STG56Q,STG78P,STG78Q,TCS3P,TCS3Q,TCS4P,TCS4Q,TCS5P,TCS5Q,TCS6P,TCS6Q,TCS7P,TCS7Q,TCS8P,TCS8Q;
    int middle=0;    

    void main()
{
	while (!fin.eof())
	{
		 //middle++;
	getline(fin, str);
    istringstream iss(str.c_str());
    if(middle==0)
	index1=str;
		//	iss>>index[i][j];
	else index2[middle-1]=str;
	middle++;
	}
	fin.close();

	std::ofstream  fout1("name.txt");  
    if(!fout1)
    {
		std::cerr << "Error opening name.txt for writing \n";
        exit(1);
    }
   fout1<<index1<<endl;
	 fout1.close();

     std::ofstream  fout2("data.txt");  
     if(!fout2)
    {
		std::cerr << "Error opening data.txt for writing \n";
        exit(1);
    }
		for(i=0;i<96;i++)
		{ 
			  fout2<<index2[i]<<endl; 
		}
	fout2.close();

     std::ifstream fin1("name.txt");
     middle=0;    
		while (!fin1.eof())
	{
		 //middle++;
		getline(fin1, str1,',');
    istringstream iss1(str1.c_str());
    	j=middle%88;
        index[0][j]=str1;
		  middle++;
		}
	fin1.close();

    std::ifstream fin2("data.txt");
    middle=0;    
		while (!fin2.eof())
	{
	  getline(fin2, str2,',');
    istringstream iss2(str2.c_str());
    i=middle/88+1;
	  j=middle%88;
    index[i][j]=str2;
	  middle++;
		}
	fin2.close();

/*    std::ofstream  fout("output.txt");  
    if(!fout)
    {
		std::cerr << "Error opening output.txt for writing \n";
        exit(1);
    }
		for(i=0;i<97;i++)
		{
          for(j=0;j<88;j++)
		  {
			  fout<<"index["<<i<<"]["<<j<<"]="<<index[i][j]<<endl;
		  }
		}
		    fout.close();
*/
  //      int i1,i2,i3;
	   string j1,j2,j3;
	   j1="CCBP";
	   j2="PNP";
	   j3="G3P";
	   for(j=0;j<88;j++)
		  {
			i=0;
			str2=index[i][j];
            if(str2==j1)
			    i1=j;
		       else if (str2==j2)
			    i2=j;
		      else if(str2==j3) 
			   i3=j;
	   }
     
    std::ofstream  fout3("time&data.txt");  
    if(!fout3)
    {
		std::cerr << "Error opening time&data.txt for writing \n";
        exit(1);
    }
         for(i=1;i<97;i++)
		 {
			 str2=index[i][0];
       fout3<<str2<<",";
		   str2=index[i][1];
       fout3<<str2<<":";
		  for(j=0;j<88;j++)
		  {
		     if(j==i1)
			   fout3<<"CCBP="<<index[i][j]<<";";
              else if(j==i2)
                fout3<<"PNP="<<index[i][j]<<";";
			    else if(j==i3)
                   fout3<<"G3P="<<index[i][j]<<";";
		  }
    fout3<<endl;
		 }
    fout3.close();
    
      for(i=1;i<97;i++)
	 {
    indexf[0][i]=atof(index[i][i1].c_str());
	  indexf[1][i]=atof(index[i][i2].c_str());
	  indexf[2][i]=atof(index[i][i3].c_str());
	  //cout<<indexf[0][i]<<","<<indexf[1][i]<<","<<indexf[2][i]<<endl;
      indexf_CCBQ+=indexf[0][i];
      indexf_PNP+=indexf[1][i];
      indexf_G3P+=indexf[2][i];
	 }
      float abc=96.0;
      indexf_CCBQ=indexf_CCBQ/abc;
      indexf_PNP=indexf_PNP/abc;
      indexf_G3P=indexf_G3P/abc;
   
      for(i=1;i<97;i++)
	 {
    indexd[0][i]=atof(index[i][i1].c_str());
	  indexd[1][i]=atof(index[i][i2].c_str());
	  indexd[2][i]=atof(index[i][i3].c_str());
	  //cout<<indexf[0][i]<<","<<indexf[1][i]<<","<<indexf[2][i]<<endl;
      indexd_CCBQ+=indexd[0][i];
      indexd_PNP+=indexd[1][i];
      indexd_G3P+=indexd[2][i];
	 }
      double abcd=96.0;
      indexd_CCBQ=indexd_CCBQ/abcd;
      indexd_PNP=indexd_PNP/abcd;
      indexd_G3P=indexd_G3P/abcd;
   
      cout<<"CCBQ_average_float="<<setprecision(18)<<indexf_CCBQ<<"  ,  ";
      cout<<"CCBQ_average_double="<<setprecision(18)<<indexd_CCBQ<<endl;
      cout<<"PNP_average_float="<<setprecision(18)<<indexf_PNP<<"  ,  ";
      cout<<"PNP_average_double="<<setprecision(18)<<indexd_PNP<<endl;
      cout<<"G3P_average_float="<<setprecision(18)<<indexf_G3P<<"  ,  ";
      cout<<"G3P_average_double="<<setprecision(18)<<indexd_G3P<<endl;

}
